/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.niedziela12.pai.ejb.room.facades;

import com.niedziela12.pai.ejb.AbstractFacade;
import com.niedziela12.pai.ejb.entities.Room;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Kamil
 */
@Stateless
@TransactionAttribute(TransactionAttributeType.MANDATORY)
public class RoomFacade extends AbstractFacade<Room> implements RoomFacadeLocal {
    @PersistenceContext(unitName = "com.niedziela12.pai.ejb")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public RoomFacade() {
        super(Room.class);
    }

    @Override
    public List<Integer> getAllFreeRooms(int roomId, Date begin, Date end, int capactiy) {
        return em.createQuery("SELECT r.id FROM room h INNER JOIN h.rooms r WHERE h.id="+roomId+" AND r.capacity="+capactiy+" AND r NOT IN (SELECT rm FROM Reservation res INNER JOIN res.room rm WHERE res.reservationBegin > "+end+" OR res.reservationEnd < "+begin+")").getResultList();
    }

    @Override
    public List<Room> getHotelRooms(Long hotelId) {
        return em.createNamedQuery("Room.findRoomsInHotel").setParameter("id", hotelId).getResultList();
    }

}
